package com.ucode.springboot.starter.db.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import cn.hutool.log.Log;

public class DruidDataSourcesConfig {

    private static Log log = Log.get(DruidDataSourcesConfig.class);
    
    public static DataSource getDataSource (DruidParam druidParam) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(druidParam.getDbUrl());
        datasource.setUsername(druidParam.getUsername());
        datasource.setPassword(druidParam.getPassword());
        datasource.setDriverClassName(druidParam.getDriverClassName());
        datasource.setInitialSize(druidParam.getInitialSize());
        datasource.setMinIdle(druidParam.getMinIdle());
        datasource.setMaxActive(druidParam.getMaxActive());
        datasource.setMaxWait(druidParam.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(druidParam.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(druidParam.getMinEvictableIdleTimeMillis());
        datasource.setMaxEvictableIdleTimeMillis(druidParam.getMaxEvictableIdleTimeMillis());
        datasource.setValidationQuery(druidParam.getValidationQuery());
        datasource.setTestWhileIdle(druidParam.isTestWhileIdle());
        datasource.setTestOnBorrow(druidParam.isTestOnBorrow());
        datasource.setTestOnReturn(druidParam.isTestOnReturn());
        datasource.setPoolPreparedStatements(druidParam.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(druidParam.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            datasource.setFilters(druidParam.getFilters());
        } catch (Exception e) {
            log.error(e, "druid configuration initialization filter");
        }
        datasource.setConnectionProperties(druidParam.getConnectionProperties());
        return datasource;
    }
    
    public static SqlSessionFactory getSqlSessionFactory(DataSource dataSource,String mapperLocations) throws Exception{
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setDataSource(dataSource);
        factory.setMapperLocations(resolver.getResources(mapperLocations));
        return factory.getObject();
    }
    
    public static DataSourceTransactionManager getTransactionManager(DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }

    public static SqlSessionTemplate getSqlSessionTemplateOne(SqlSessionFactory sessionFactory) throws Exception {
        return new SqlSessionTemplate(sessionFactory);
    }
}
